import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class PlayerLoadPanel extends JFrame
{
	
	private static Player player;
	
	JLabel lblPlayer = new JLabel("player");
	JComboBox boxPlayer = new JComboBox();
	JTextField txtPlayer = new JTextField();
	
	JLabel lblWeapon = new JLabel("Weapon");
	JComboBox boxWeapon = new JComboBox();
	JTextField txtWeapon = new JTextField();
	
	JLabel lblArmor = new JLabel("Armor");
	JComboBox boxArmor = new JComboBox();
	JTextField txtArmor = new JTextField();
	
	public PlayerLoadPanel(Player player)
	{
		
		setTitle("load");
		setSize(500,180);
		setVisible(true);
		
		GridLayout layout = new GridLayout(4,3);
		
		JButton btnLoad = new JButton("load");
		
		setLayout(layout);
		
		add(lblPlayer);
		
		boxPlayer.addItem("do not load");
		boxPlayer.addItem("normal player");
		add(boxPlayer);
		
		txtPlayer.setEditable(true);
		add(txtPlayer);
		
		add(lblWeapon);
		
		boxWeapon.addItem("do not load");
		boxWeapon.addItem("normal weapon");
		boxWeapon.addItem("posion weapon");
		add(boxWeapon);
		
		txtWeapon.setEditable(true);
		add(txtWeapon);
		
		add(lblArmor);
		
		boxArmor.addItem("do not load");
		boxArmor.addItem("normal armor");
		add(boxArmor);
		
		txtArmor.setEditable(true);
		add(txtArmor);
		
		add(btnLoad);
		
		btnLoad.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				load();
			}	
		});
		
	}
	
	private void load()
	{
		switch(boxPlayer.getSelectedIndex())
		{
			case 0://do not load
				break;
			case 1: //normal player
			try 
			{
					player = Database.loadPlayer(txtPlayer.getText());
					PlayerGUI.addMessage("loaded player");
			}
			catch (SQLException e)
			{
				PlayerGUI.addMessage("error loading player");
			}
				break;
		}
		
		switch(boxWeapon.getSelectedIndex())
		{
			case 0: //do not load
				break;
			case 1: //normal weapon
				try 
				{ 
					player.setWeapon(Database.loadWeapon(txtWeapon.getText()));
					PlayerGUI.addMessage("loaded weapon");
				}
				catch(Exception e)
				{
					PlayerGUI.addMessage("error loading weapon");
				}
				break;
			case 2: //poison weapon
				try 
				{ 
					player.setWeapon(Database.loadPosionWeapon(txtWeapon.getText()));
					PlayerGUI.addMessage("loaded poison weapon");
				}
				catch(Exception e)
				{
					PlayerGUI.addMessage("error loading weapon");
				}
				break;
		}
		
		switch(boxArmor.getSelectedIndex())
		{
			case 0://do not load
				break;
			case 1: //normal armor
				try 
				{
					player.setArmor(Database.loadArmor(txtArmor.getText()));
					PlayerGUI.addMessage("loaded armor");
				}
				catch (SQLException e)
				{
					PlayerGUI.addMessage("error loading armor");
				}
				break;
		}
		
		PlayerGUI.updatePlayer(player);
		this.dispose();
	}
	
}
